package com.bdc.whatwhat.Service;

import android.util.Log;

import com.bdc.whatwhat.modele.MonFlux;
import com.bdc.whatwhat.modele.Notification;
import com.bdc.whatwhat.modele.WhatWhat;
import com.bdc.whatwhat.modele.Whatter;
import com.bdc.whatwhat.session.SessionManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import retrofit.RestAdapter;

/**
 * Created by florentchampigny on 18/04/2014.
 */
public class Service {

    public static class NetworkException extends Exception {
        public NetworkException(Throwable throwable) {
            super(throwable);
        }
    }

    public static WhatWhatService getService() {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(WhatWhatService.ENDPOINT)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setLog(new RestAdapter.Log() {
                    @Override
                    public void log(String message) {
                        Log.d("retrofit", message);
                    }
                })
                .build();

        WhatWhatService service = restAdapter.create(WhatWhatService.class);
        return service;
    }

    static String format(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date());
    }

    public static String connexion(String email, String origin, String id,String token, String nom, String prenom,String os,String jeton) throws NetworkException {
        try {
            return getService().connexion(email,origin, id,token, nom, prenom,os,jeton);
        } catch (Exception e) {
            e.printStackTrace();
            throw new NetworkException(e);
        }
    }

    public static List<WhatWhat> flux(Integer page,String token) throws NetworkException {
        try {
            return getService().flux(page, token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static MonFlux monFlux(String token) throws NetworkException {
        try {
            return getService().monFlux(token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static Whatter monProfil(String token) throws NetworkException {
        try {
            return getService().monProfil(token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<Whatter> mesAmis(Integer page,String token) throws NetworkException {
        try {
            return getService().mesAmis(page,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<Whatter> mesDemandes(Integer page,String token) throws NetworkException {
        try {
            return getService().mesDemandes(page,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<Whatter> ajouterAmi(String token,String id) throws NetworkException {
        try {
            return getService().ajouterAmi(token,id);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static String refuserAmi(String token, String id) throws NetworkException {
        try {
            return getService().refuserAmi(token,id);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static Integer nombreDeDemandes(String token) throws NetworkException {
        try {
            return getService().nombreDeDemandes(token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<Notification> mesNotifications(String token) throws NetworkException {
        try {
            return getService().mesNotifications(token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<WhatWhat> recherche(String phrase, Integer page,String token) throws NetworkException {
        try {
            return getService().recherche(phrase, page,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static List<Whatter> rechercheUtilisateur(String phrase, Integer page,String token) throws NetworkException {
        try {
            return getService().rechercheUtilisateur(phrase, page,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static WhatWhat envoyerWhatWhatExistant(String id,String token) throws NetworkException {
        try {
            return getService().envoyerWhatWhatExistant(id,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static WhatWhat envoyerWhatWhat(String phrase,String token) throws NetworkException {
        try {
            return getService().envoyerWhatWhat(phrase,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static void demandeWhatWhat(String utilisateur,String token) throws NetworkException {
        try {
            getService().demandeWhatWhat(utilisateur,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }

    public static void aimer(String id,String token) throws NetworkException {
        try {
            getService().aimer(id,token);
        } catch (Exception e) {
            throw new NetworkException(e);
        }
    }


}